<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
		<title></title>
		<link rel="stylesheet" type="text/css" href="../../css/api.css" />
		<link rel="stylesheet" type="text/css" href="../../css/style.css" />
		<style type="text/css">
			html,body{ background-color: #F7F8FA; height:100%; }
			.add_friends .top{ padding: 7px 15px; background-color: #fff; margin-top:10px;}
			.add_friends .top img{ width: 30px; margin-right: 10px;}
			.add_friends .top input{ display:block; border: 0; font-size: 15px; color: #333; width: 100%; padding: 7px 0;}
			.add_friends h2{ display:block; padding: 10px 15px 0; font-size: 13px; line-height: 30px; font-weight: 400; color: #666;}
			.add_friends .list{ background-color: #fff; padding: 0 15px;}
			.add_friends .list li{ position:relative; padding: 5px 0;}
			.add_friends .list li::after{ position: absolute; content: ""; left: 0; right: -15px; bottom: 0; border-bottom: 1px #d5d5d5 solid; transform: scaleY(0.5);}
			.add_friends .list img{ width: 35px; height: 35px; border-radius: 4px; margin-right: 10px;}
			.add_friends .list h4{ display:block; position: relative; font-size: 15px; font-weight: 400; color: #333; line-height: 20px;}
			.add_friends .list p{ font-size: 12px; color: #999; padding-top: 1px;}
			.add_friends .list a{ display:inline-block; margin-left: 8px; font-size: 13px; color: #9f9f9f; line-height: 25px; width: 50px; text-align: center;}
			.add_friends .list .tg{ color:#5D5D5D;}
			.add_friends .list .on01{ background-color:#3bcb9f; color: #fff; border-radius: 4px;}
			.add_friends .list .on02{ background-color:#f7f8fa; color: #222; border-radius: 4px;}
			.sk-three-bounce {
			  margin: 0 auto;
			  width: 80px;
			  text-align: center; }
			  .sk-three-bounce .sk-child {
				width: 13.5px;
				height: 13.5px;
				background-color: #3262F1;
				border-radius: 100%;
				display: inline-block;
				-webkit-animation: sk-three-bounce 1.4s ease-in-out 0s infinite both;
						animation: sk-three-bounce 1.4s ease-in-out 0s infinite both; }
			  .sk-three-bounce .sk-bounce1 {
				-webkit-animation-delay: -0.32s;
						animation-delay: -0.32s; }
			  .sk-three-bounce .sk-bounce2 {
				-webkit-animation-delay: -0.16s;
						animation-delay: -0.16s; }

			@-webkit-keyframes sk-three-bounce {
			  0%, 80%, 100% {
				-webkit-transform: scale(0);
						transform: scale(0); }
			  40% {
				-webkit-transform: scale(1);
						transform: scale(1); } }

			@keyframes sk-three-bounce {
			  0%, 80%, 100% {
				-webkit-transform: scale(0);
						transform: scale(0); }
			  40% {
				-webkit-transform: scale(1);
						transform: scale(1); }
			}
			.display-none{ display:none !important; }
			.text-center { text-align: center !important; }
		</style>
	</head>
	<body>
		<div class="add_friends">
			<ul class="top" tapmode="lipress" onclick="opWin('search_win','search_win.html',true,{},false);">
				<li class="m_cell_hd"><img src="../../image/img_29.png" alt="" /></li>
				<li class="m_cell_ft m_cell_primary"><input type="text" value="" placeholder="要添加的手机号" readonly /></li>
			</ul>
			<h2 id="tongxunlu" class="display-none">申请列表</h2>
			<ul class="list" id="friendslist">
			</ul>
			<script id="friendslist_template" type="text/template">
			{{ for (var i = 0, len = it.length; i < len; i++) { }}
				<li>
					<div class="m_cell_hd"><img data-url="{{=it[i].avatar}}" onload="fnLoadImage(this)" src="../../image/defaultimg.png" onerror="javascript:this.src='../../image/defaultimg.png';" /></div>
					<div class="m_cell_hd m_cell_primary">
						<h4>{{=it[i].realname}}</h4>
						<p>{{=it[i].msg}}</p>
					</div>
					<div class="m_cell_ft">
						{{? it[i].status == 0}}
						<a href="javascript:;" tapmode="btnpress" data-id="{{=it[i].id}}" onclick="passfriend(this,1);" class="on01">通过</a>
						<a href="javascript:;" tapmode="btnpress" data-id="{{=it[i].id}}" onclick="passfriend(this,2);" class="on02">拒绝</a>
						{{?? it[i].status == 1}}
						<a href="javascript:;" class="tg">已通过</a>
						{{?? it[i].status == 2}}
						<a href="javascript:;">已拒绝</a>
						{{?? it[i].status == 3}}
						<a href="javascript:;">已失效</a>
						{{?}}
					</div>
				</li>
			{{ } }}
			</script>
		</div>
	</body>
	<script type="text/javascript" src="../../script/api.js"></script>
	<script type="text/javascript" src="../../script/app.js"></script>
	<script type="text/javascript" src="../../script/doT.js"></script>
	<script type="text/javascript">
		var page = 1;
		var UILoading;
		apiready = function() {
			api.parseTapmode();
			UILoading = api.require('UILoading');
			$api.css($api.dom('.add_friends'), 'display: none');
			showloading();
			getJsonFromAPI();
			
			api.addEventListener({
				name : 'reloadpage'
			}, function(ret, err) {
				refreshPage();				
			});
			api.addEventListener({
                name: 'navitembtn'
            }, function(ret, err) {
                if (ret.type == 'left') {
					api.closeWin({
					});
                }
            });
		}
		function passfriend(obj,status) {
			//通过OR拒绝
			api.showProgress({
				title: '请稍后...',
				text: '',
				modal: true
			});
			var pid = $api.attr(obj,'data-id');
			api.getPrefs({
				key: 'user_info'
			}, function(ret, err) {
				if (ret.value) {
					var userinfo = JSON.parse(ret.value);
					api.ajax({
						url: ApiUrl + '/v1/friendlogs',
						method: 'post',
						cache: false,
						headers: {
							authorization: kAppKey+';'+userinfo.access_token+';'+userinfo.uid
						},
						data: {
							values: {
								pid: pid,
								type: 'changestatus',
								status: status
							}
						}
					}, function(ret, err) {
						api.hideProgress();
						if (ret) {
							api.sendEvent({
								name : 'refreshfriends'
							});
							if(status == 1)
							{
								$api.html(obj.parentNode,'<a href="javascript:;" class="tg">已通过</a>');
								//toast('已添加为好友');
							}
							else if(status == 2)
							{
								$api.html(obj.parentNode,'<a href="javascript:;">已拒绝</a>');
								//toast('已拒绝好友申请');
							}
						} else {
							if(err.statusCode==402)
							{
								Relogin();
							}
							else
							{
								console.log(JSON.stringify(err));
								//toast('网络异常');
							}
						}
					});
				}
			});
		}
		function fnLoadImage(obj) {
			obj.onload = null;
			var dataUrl = $api.attr(obj, 'data-url');
			if(dataUrl!=''){
				api.imageCache({
					url: dataUrl
				}, function(ret, err) {
					if(ret && ret.status == true){
						obj.src = ret.url;
					}
				});
			}
		}
		
		function refreshPage() {
			page = 1;
			getJsonFromAPI();
		}
		function getJsonFromAPI() {
			//从接口获取查询数据
			api.getPrefs({
				key: 'user_info'
			}, function(ret, err) {
				if (ret.value) {
					var userinfo = JSON.parse(ret.value);
					api.ajax({
						url: ApiUrl + '/v1/friendlogs/'+page,
						method: 'get',
						cache: false,
						timeout: 10,
						headers: {
							authorization: kAppKey+';'+userinfo.access_token+';'+userinfo.uid
						}
					}, function(ret, err) {
						//api.refreshHeaderLoadDone();
						hideloading();
						$api.css($api.dom('.add_friends'), 'display: block');
						if (ret) {
							
							if(ret.list.length > 0)
							{
								$api.removeCls($api.byId('tongxunlu'),'display-none');
								var isAppend = true;
								if(page==1)
								{
									isAppend = false;
								}
								page++;
								var friendslist = ret.list;
								UpdateList('friendslist','friendslist_template',friendslist,isAppend);
							}
							else
							{
								if(page==1)
								{
									UpdateList('friendslist','friendslist_template',ret.list,false);
									$api.addCls($api.byId('tongxunlu'),'display-none');
								}
							}
							
						} else {
							if(err.statusCode==402)
							{
								Relogin();
							}
							else
							{
								console.log(JSON.stringify(err));
								toast('网络异常');
							}
						}
					});
				}
			});
		}
		//渲染数据
		function UpdateList(domId,templateId,jsondata,isAppend) {
			//console.log(JSON.stringify(jsondata));
			var act_content = $api.byId(domId);
			var tempFn = doT.template($api.byId(templateId).innerHTML);
			var resultText = tempFn(jsondata);
			//alert(isAppend);
			console.log(resultText);
			if(isAppend)
			{
				$api.append(act_content, resultText);
			}
			else
			{
				$api.html(act_content, resultText);
			}
			api.parseTapmode();
		}
	</script>
</html>